package com.hysteria.game;
import java.util.Random;

import com.hysteria.game.Cell;
import com.hysteria.game.Cell.CellType;

public class Map 
{
	private int xTiles;
	private int yTiles;

	
	private Cell mapGrid[][];
	public Cell[][] getGrid(){ return mapGrid; }
	
	public Map(int x, int y)
	{
		this.xTiles=x;
		this.yTiles=y;
		mapGrid = new Cell[x][y];
	}
	
	public int getGridFromWorld(float worldPos){
		int world = Math.round(worldPos);
		return getGridFromWorld(world);
	}
	
	public int getGridFromWorld(int worldPos){
		return worldPos/Cell.cellSize;
	}
	
	public Cell getCell(int gridX, int gridY){
		if(gridX < 0 || gridX >= mapGrid.length || gridY <0 || gridY >= mapGrid[0].length){
			return null;
		}
		if(mapGrid[gridX] != null){
			return mapGrid[gridX][gridY];
		}
		
		return null;
	}
	
	public void generate()
	{
		for(int i=0; i<this.xTiles; i++)
			for(int j=0;j<this.yTiles;j++)
			{
				int temp=0;
				Random rand = new Random();
				temp=rand.nextInt(11); 
				
				if(temp>=6)
				{
					mapGrid[i][j] = new Cell(CellType.GROUND,i,j);
				}
				else 
					mapGrid[i][j] = new Cell(CellType.WALL, i,j);   //Wall
				/*if(i==0 || i==this.xTiles-1)
					grid[i][j] = '@';	//Immutable wall
				if(j==0 || j== this.yTiles-1)
					grid[i][j] = '@';*/
			}
		
	}
	
	public void CApass()
	{
		int adjacentTiles=0;
		for(int i=1; i<this.xTiles-2;i++)
			for(int j=1;j<this.yTiles-2;j++)
			{
				
				//Dont touch this
				if(i!=0 && mapGrid[i-1][j].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(mapGrid[i+1][j].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(j!=0 && mapGrid[i][j-1].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(mapGrid[i][j+1].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(mapGrid[i+1][j+1].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(j!=0 && i!=0 && mapGrid[i-1][j-1].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(j!=0 && mapGrid[i+1][j-1].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(i!=0 && mapGrid[i-1][j+1].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				//If there are more walls then create a floor
				if(adjacentTiles<3)
					mapGrid[i][j].setCellType(CellType.GROUND);
				adjacentTiles=0;
				
				//Also dont touch this
				if(i!=0 && mapGrid[i-1][j].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(mapGrid[i+1][j].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(j!=0 && mapGrid[i][j-1].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(mapGrid[i+1][j+1].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(j!=0 && i!=0 && mapGrid[i-1][j-1].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(j!=0 && mapGrid[i+1][j-1].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				if(i!=0 && mapGrid[i-1][j+1].getCellType().equals(CellType.WALL))
					adjacentTiles++;
				
				//You can now procede to touch things
				if(adjacentTiles>7)
					mapGrid[i][j].setCellType(CellType.GROUND);
				adjacentTiles=0;
				
				
			}
	}
	
	
}
